﻿Public Partial Class Advertisements
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            LoadAdvertisements()
        End If
    End Sub

    Private Sub LoadAdvertisements()
        Dim manager As New BLL.AdvertisementManager

        gvAdvertisements.DataSource = manager.GetAllAdvertisements()
        gvAdvertisements.DataBind()
    End Sub

    Private Sub gvAdvertisements_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvAdvertisements.RowCancelingEdit
        gvAdvertisements.EditIndex = -1
        gvAdvertisements.DataBind()
    End Sub

    Private Sub gvAdvertisements_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gvAdvertisements.RowEditing
        LoadAdvertisements()

        gvAdvertisements.EditIndex = e.NewEditIndex
        gvAdvertisements.DataBind()
    End Sub

    Private Sub gvAdvertisements_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvAdvertisements.RowUpdating
        Dim gv As GridView
        Dim idx As Integer
        Dim cell As DataControlFieldCell
        ' Extracts all new values from the gridview
        gv = CType(sender, GridView)
        For idx = 0 To gv.Columns.Count - 1
            cell = CType(gv.Rows(e.RowIndex).Cells(idx), DataControlFieldCell)
            gv.Columns(idx).ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, True)
        Next idx

        Dim manager As New BLL.AdvertisementManager


        Dim advertisement As Domain.dnc_Advertisement = manager.GetAdvertisementByID(CInt(gvAdvertisements.DataKeys(e.RowIndex).Item("ID")))
         
        ' Update Values
        advertisement.Active = Convert.ToBoolean(e.NewValues(0))
        advertisement.Name = Convert.ToString(e.NewValues(1))
        advertisement.URL = Convert.ToString(e.NewValues(2))
        advertisement.AlternateText = Convert.ToString(e.NewValues(3))
        advertisement.startDate = Convert.ToDateTime(e.NewValues(4))
        advertisement.EndDate = Convert.ToDateTime(e.NewValues(5))

        Dim postedFile As HttpPostedFile = DirectCast(gvAdvertisements.Rows(e.RowIndex).Cells(3).FindControl("upImage"), FileUpload).PostedFile

        If postedFile.InputStream.Length > 0 Then
            ' Then a new file has been loaded to be updated
            Dim arrContent(CInt(postedFile.InputStream.Length - 1)) As Byte
            postedFile.InputStream.Read(arrContent, 0, CInt(postedFile.InputStream.Length))

            Dim imageManager As New BLL.ImageManager

            ' Do Update
            Dim image As Domain.dnc_Image = imageManager.GetImageByID(advertisement.ImageID)

            ' Update Properties
            image.Name = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("\") + 1)
            image.Image = arrContent

            imageManager.UpdateImage(image)
        

        End If

        manager.Update(advertisement)

        gvAdvertisements.EditIndex = -1

        LoadAdvertisements()

    End Sub


End Class